<!-- 面包屑导航组件 -->
<template>
  <div class="container">
    <i class="el-icon-s-fold" @click="emitFn()"></i>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item
        :to="{ path: '/' }"
        v-for="(item, index) in breadArr"
        :key="index"
        >{{ item.title }}</el-breadcrumb-item
      >
    </el-breadcrumb>
  </div>
</template>

<script>
// 引入eventBus
import eventBus from "@/utils/eventBus.js";
export default {
  name: "",
  props: [""],
  data() {
    return {
      isCollapse: true,
      breadArr: [], // 面包屑数组
    };
  },

  components: {},

  computed: {},

  created() {
    // console.log(this.$route);
  },

  mounted() {},

  methods: {
    emitFn() {
      this.isCollapse = !this.isCollapse;
      eventBus.$emit("transfer", this.isCollapse);
    },
  },
  watch: {
    $route: {
      handler(val) {
        // val 为$route 改变后的值
        // 制作面包屑导航 利用了$route.matched 该属性中包含了面包屑所需要的路由信息
        // console.log(val);
        // 先清空breadArr
        this.breadArr = [];
        val.matched.forEach((item, index) => {
          if (index > 0) {
            this.breadArr.push({
              title: item.meta.title,
              path: item.path,
            });
          }
        });
        console.log(this.breadArr);
      },
      immediate: true, //立即马上监听,初始化的时候也可以实现监听
      deep: true, // 深度监听,对于数据结构嵌套深层次,那就需要加上deep
    },
  },
};
</script>
<style lang="less" scoped>
/* @import url(); 引入css类 */
// @mainColor: #eaebec; //正确的
.container {
  height: 30px;
  width: 100%;
  display: flex;
  align-items: center;
  // background: @mainColor; // @mainColor
  i {
    line-height: 30px;
    font-size: 20px;
    cursor: pointer;
    padding-right: 20px;
  }
}
</style>
